<?php
class Index extends Admin_Controller {
    const LIMIT_AUTOCOMPLETE = 5;
    public function __construct() {
        parent::__construct();
        $this->_load();
    }

    public function index() {
        $this->_replace_phrase();
    }
    
    public function ajax_save() {
        $apost = $this->input->post('tag');   
        $this->load->library('form_validation', null, 'validation');
        $this->validation->set_rules($this->tag->get_rule(empty($apost['id'])? false: $apost['id'],
                empty($apost['slug']) ? false : $apost['slug']));
        $this->validation->set_error_delimiters('<div class="error">', '</div>');

        if ($this->validation->run() === false) {
            $this->return_fail_json($this->tag->get_validation_error());
            return false;
        }
        
        if ((bool)$apost) {
            $this->tag->set(array(
                'id'   => $apost['id'],
                'name' => $apost['name'],
                'slug' => $apost['slug'],
                'description' => $apost['description']
            ));
            //Save and edit in this.
            $svar_message = "phrase var='tag.add_new_tag_successfully'";
            
            if($apost['id']){
                $svar_message = "phrase var='tag.update_tag_successfully'";
            }
                
            if ((bool) $areturn = $this->tag->save()) {
                $this->return_success_json($areturn, $this->lang->line($svar_message));
                return true;
            }
        }
    }

    public function ajax_get_list($ioffset = null) {
        $ioffset = !empty($ioffset) ? $ioffset : 1;
        $asearch = $this->input->post('search');
        $auser = $this->tag->get_list_admin($ioffset, $asearch);

        //Get list of page.
        $apage = $this->tag->get_pager('tag/admincp/index/ajax_get_list');
        $apage = $this->tag->get_pager_admin($apage);

        $areturn = array(
            'list' => $auser,
            'pages'    => $apage
        );

        $this->return_success_json($areturn, '');
    }

    public function ajax_delete() {
        $aid = $this->input->post('item_ids');
        $icur_page = $this->input->post('cur_page');
        $aid = (array) json_decode($aid);

        if ((bool) $this->tag->delete($aid)) {
            $aresult = $this->tag->get_list_admin((int) $icur_page);
            //Prepare in case if delete all items in a page.
            if (!empty($aresult)) {
                $this->return_success_json(array('page' => $icur_page), '');
            } else {
                $this->return_success_json(array('page' => $icur_page - 1), '');
            }
            return true;
        }

        $this->return_fail_json(NULL, '');
    }

    public function ajax_get() {
        $iid = $this->input->post('item_id');
        if ((bool) $oget = $this->tag->get_by_id($iid, false)) {
            $this->return_success_json($oget, '');
            return true;
        }

        $this->return_fail_json(NULL, '');
    }

    public function ajax_sort() {
        $acolumn = $this->input->post('column');
        $stype_sort = $this->tag->sort($acolumn);
        $this->return_success_json(array('type_sort' => $stype_sort), '');
    }

    public function ajax_set_view() {
        $iNumView = $this->input->post('num_view');

        if ($iNumView) {
            $this->session->set_userdata($this->config->item('ele_per_page'), $iNumView);
            $this->return_success_json(array('num_view' => $iNumView), '');

            return true;
        }

        $this->return_fail_json(array('num_view' => $iNumView), '');
    }
    
    public function ajax_get_auto_complete_data() {
        $skeyword = trim($this->input->get('term'));
        $akeyword = explode(',', $skeyword);
        $this->load->model('tag', 'tag', false, 'tag');
//        $adata = $this->tag->gets($skeyword, self::LIMIT_AUTOCOMPLETE);
        $adata = $this->tag->gets(trim($akeyword[count($akeyword) - 1]), self::LIMIT_AUTOCOMPLETE);
        //Sort array by name.
        if(sort($adata)){
            echo json_encode($adata);
            return true;
        }
    }

    public function get_sort() {
        $this->_sort = $this->session->userdata($this->config->item('sort'));

        if (!(bool) $this->_sort) {
            return array('order' => 'asc');
        }
    }

    private function _load() {
        $this->load->library('pagination');
        $this->load->helper(array('url', 'form'));
        $this->load->model('tag');
        $this->lang->load('admin/general', $this->_slang_name);
        $this->lang->load('admin/tag', $this->_slang_name);
    }

    private function _replace_phrase() {
        $this->_adata['token'] = (object) array("name" => $this->security->get_csrf_token_name(), "value" => $this->security->get_csrf_hash());
        //Get sort of product.
        $this->_adata['aSort'] = $this->tag->get_sort();
        $this->_adata['phrase_js'] = json_encode($this->lang->language);
        $this->fill_phrase('index', $this->_adata, $this->lang->language);
    }
}
?>
